package backend.model.worklog;

import backend.model.admin.Employee;
import backend.model.base.DBModel;
import org.hibernate.annotations.NamedQueries;
import org.hibernate.annotations.NamedQuery;

import javax.persistence.*;
import java.util.Date;

@NamedQueries({
        @NamedQuery(name = "loadWorkLogMismatchForDateAndEmployee", query = "from WorkLogMismatch where employee.id = :employeeId and date = :date"),
        @NamedQuery(name = "loadWorkLogMismatchForTimeInterval", query = "from WorkLogMismatch where date >= :startDate and date <= :endDate"),
        @NamedQuery(name = "loadWorkLogMismatchForTimeIntervalAndEmployee", query = "from WorkLogMismatch where employee.id = :employeeId and date >= :startDate and date <= :endDate"),
        @NamedQuery(name = "loadWorkLogMismatchForTimeIntervalAndTeam", query = "from WorkLogMismatch where employee.team.id = :teamId and date >= :startDate and date <= :endDate")
})

@Entity
@Table(name = "work_log_mismatch")
public class WorkLogMismatch extends DBModel {

    @Id
    @GeneratedValue
    @Column(name = "id")
    private int id;

    @Column(name = "date")
    private Date date;

    @ManyToOne
    @JoinColumn(name = "employee_id")
    private Employee employee;

    @Column(name = "local_hours")
    private int localHours;

    @Column(name = "charisma_hours")
    private int charismaHours;

    @Override
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }

    public int getLocalHours() {
        return localHours;
    }

    public void setLocalHours(int localHours) {
        this.localHours = localHours;
    }

    public int getCharismaHours() {
        return charismaHours;
    }

    public void setCharismaHours(int charismaHours) {
        this.charismaHours = charismaHours;
    }

    @Override
    public boolean getIsActive() {
        return true;
    }

    @Override
    public String getDisplayLabel() {
        return "WorkLogMismatch: " + getDate() + " ( Local: "+ getLocalHours() + ", Charisma: " + getCharismaHours() + ")";
    }

    @Override
    public int compareTo(Object o) {
        int ret = 0;
        if (!(o instanceof WorkLogMismatch)) {
            ret = -1;
        } else {
            WorkLogMismatch otherWorkLogMismatch = (WorkLogMismatch) o;
            ret = getDate().compareTo(otherWorkLogMismatch.getDate());
            if (ret == 0) {
                ret = getEmployee().getDisplayLabel().compareTo(otherWorkLogMismatch.getEmployee().getDisplayLabel());
            }
        }
        return ret;
    }
}
